package com.asset.controller;

import com.asset.common.BaseResponse;
import com.asset.common.ErrorCode;
import com.asset.common.PageRequest;
import com.asset.common.ResultUtils;
import com.asset.exception.ThrowUtils;
import com.asset.model.dto.allocate.AllocateQuery;
import com.asset.model.vo.AllocateVO;
import com.asset.model.vo.AseetTranferVo;
import com.asset.model.vo.LoginUserVO;
import com.asset.service.AllocateService;
import com.asset.service.CompanyService;
import com.asset.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

// 封装 PageRequest 和 AllocateQuery 的类

/**
 * 台账
 */
@RestController
@RequestMapping("/allocateController")
public class AllocateController {
    @Resource
    private AllocateService allocateService;
    @Resource
    private UserService userService;

    @Resource
    private CompanyService companyService;

    /**
     * 分页查询资产调拨列表
     *
     * @param allocateRequest 包含分页请求参数和调拨查询条件
     * @return 调拨列表分页数据
     */
    @PostMapping("/transfer")
    public BaseResponse<PageInfo<AllocateVO>> allocate(@RequestBody AllocateQuery allocateRequest) {
//        ThrowUtils.throwIf(allocateRequest == null, ErrorCode.PARAMS_ERROR);
        PageHelper.startPage(allocateRequest.getCurrent(),allocateRequest.getPageSize());
        // 调用服务层查询，返回分页结果
//        BaseResponse<AllocateVO> result = allocateService.allocateSelectAll(allocateRequest);
        List<AllocateVO> result =allocateService.getAllocate(allocateRequest);
        if(result==null){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR,"未查询到数据");
        }
        PageInfo<AllocateVO> pageInfo = new PageInfo<>(result);
        return ResultUtils.success(pageInfo);
    }

    /**
     * 查询资产调拨详情
     *

     * @return 调拨详情列表
     */
    @PostMapping("/transferDetail")
    public BaseResponse<PageInfo<AseetTranferVo>> transferDetail(Integer transferId){
//        ThrowUtils.throwIf(transferId == null, ErrorCode.PARAMS_ERROR);
        PageHelper.startPage(new PageRequest().getCurrent(),new PageRequest().getPageSize());
        // 调用服务层查询，返回分页结果
        List<AseetTranferVo> result =allocateService.getAllocateById(transferId);
        if(result==null){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR,"未查询到数据");
        }
        PageInfo<AseetTranferVo> pageInfo = new PageInfo<>(result);
        return ResultUtils.success(pageInfo);
    }

    /**
     * 点击新增调拨按钮
     *,需要有用当前用户的信息，以及
     * @return 调
     */
    @GetMapping("clickBtn")
    public BaseResponse<HashMap<LoginUserVO, PageInfo<AseetTranferVo>>> clickBtn(){
        //获取刀用户的信息
        HashMap<LoginUserVO, PageInfo<AseetTranferVo> > map = new HashMap<>();
        LoginUserVO loginUserVOByTK = userService.getLoginUserVOByTK();
        System.out.println(loginUserVOByTK);
        if(loginUserVOByTK==null){
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR);
        }
        int companyId = loginUserVOByTK.getCompanyId();//获取到公司id
        Integer companyType = companyService.selectType(companyId);//获取公司类型id  1为集团，2为子公司，3为分公司
        if(companyType == null){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }else{
            if(companyType == 1||companyType==2){
                PageHelper.startPage(new PageRequest().getCurrent(),new PageRequest().getPageSize());
                // 调用服务层查询，返回分页结果
                List<AseetTranferVo> result =allocateService.clickBtn(companyId);
                if(result==null){
                    return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR,"未查询到数据");
                }
                PageInfo<AseetTranferVo> pageInfo = new PageInfo<>(result);
                map.put(loginUserVOByTK,pageInfo);
            }
        }
        return ResultUtils.success(map);
    }

}